// 页面加载完成后初始化
$(document).ready(function () {
	// 从URL参数获取书籍ID和章节号
    const urlParams = new URLSearchParams(window.location.search);
    const bookId = urlParams.get('bookId');
    const chapterNum = urlParams.get('chapterNum');
    const bookName = urlParams.get('bookName');
    const bookWriter = urlParams.get('bookWriter');
    // 如果URL参数存在，则获取章节详情
    if (bookId && chapterNum) {
        getChapterDetail(bookId, chapterNum,bookName,bookWriter);
    }
    getChapterDetail(bookId, chapterNum,bookName,bookWriter);
});

// 获取章节详情的函数
function getChapterDetail(bookId, chapterNum,bookName,bookWriter){
	// 显示加载状态
//    showLoading(true);
	
	 $.ajax({
		 type: "POST",
         url: 'bookContent.do',
         data: {
             bookId: bookId,
             chapterNum: chapterNum
         },
         dataType: "json",
         success: function(data){
        	 let bookData = data.data.data[0];
        	 if (bookData != null){
        		 console.log(bookData);
        		 $("h2").html(bookData.chapterName);
        		 $(".chapter-tips li:nth-of-type(2)").html(bookName);
        		 $(".chapter-tips li:nth-of-type(4)").html(bookWriter);
        		 $(".chapter-tips li:nth-of-type(6)").html(bookData.wordCount);
        		 const date = parseLocalDateTime(bookData.updateTime);
        		 console.log(date.toLocaleString());
        		 $(".chapter-tips li:nth-of-type(8)").html(date);
        		 $(".article").html(bookData.content)
        	 }
         }
	 });
}

function parseLocalDateTime(json) {
	console.log(json);
    return new Date(
        json.year, 
        json.monthValue - 1, // JavaScript 月份从 0 开始
        json.dayOfMonth,
        json.hour,
        json.minute,
        json.second,
        json.nano / 1000000 // 转换为毫秒
    );
}